# coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse
import os, json
from shareFun.process import operate_img


def indextest(request):
    return render(request, "yuan.html")


def indexHTML(request):
    return render(request, "index.html")


def getpicture1(request, num):
    pageData = {
        "title": "哦？",
        "pageCos": num,
    }
    return render(request, "showCos.html", {"pageData": pageData})


def getpicture2(request):
    pageData = {
        "title": "喵！",
    }
    return render(request, "showCos.html", {"pageData": pageData})


def getpicture3(request):
    pageData = {
        "title": "嘿:)",
    }
    return render(request, "showCos.html", {"pageData": pageData})


def getgraduate(request):
    return render(request, "skill.html")


def love_read(request):

    res = {
        'a': 1
    }

    return HttpResponse(json.dumps(res))


def process_image(request):
    if request.method == "POST":
        step = request.POST.get('step')
        print("当前运行步骤", step)
        if step == "1":  # 接收到的客户端数据，执行对应操作
            part1 = json.loads(request.POST.get('part1'))
            print("part1", part1)
            image_src = request.POST.get('image')
            pce = operate_img(image_src)
            res = { "status": 1, "result": "success" }
            res["gaussian_image"] = pce.gaussian_blur(int(part1["gaussian_kernel"]))
            res["median_image"] = pce.median_blur(int(part1["median_kernel"]), part1["median_checked"])
            res["sobel_image"] = pce.sobel_blur(int(part1["sobel"]), part1["isVertical"])
            res["threshold_image"] = pce.threshold_img(int(part1["threshold"]), part1["otsu_checked"])
            res["dilate_image"] = pce.dilate_img(int(part1["dilate_kernel"]["x"]), int(part1["dilate_kernel"]["y"]),
                                                 int(part1["dilate_kernel"]["times"]))
            res["erode_image"] = pce.erode_img(int(part1["erode_kernel"]["x"]), int(part1["erode_kernel"]["y"]),
                                               int(part1["erode_kernel"]["times"]))
        elif step == "2":
            part2 = json.loads(request.POST.get('part2'))
            print("part2", part2)
            image_src = request.POST.get('image')
            image_src_ter = request.POST.get('image_ter')
            pce = operate_img(image_src, image_src_ter, 'ternimal')
            res = {
                "status": 1,
                "result": "success",
            }
            res["counter_image"], res["rect_image"] = pce.find_counters(int(part2["minSpecific"]),
                                                                        int(part2["maxSpecific"]))
            res["num_plate_image"], res["final_num_plate_image"] = pce.unify_possible_pos()
        elif step == "3":
            part3 = json.loads(request.POST.get('part3'))
            print("part3", part3)
            image_np_fl = part3["num_plate_image"]
            pce = operate_img(image_np_fl, load_which='wordsplit')
            res = {
                "status": 1,
                "result": "success",
            }
            res["split_process_img"], res["split_result"] = pce.word_split()
            res["result_img"], res["result_word"] = pce.predict()
            print(res)
        return HttpResponse(json.dumps(res))
